.\"	$NetBSD: pci.3,v 1.10 2014/12/16 13:38:36 msaitoh Exp $
.\"
.\" Copyright 2001 Wasabi Systems, Inc.
.\" All rights reserved.
.\"
.\" Written by Jason R. Thorpe for Wasabi Systems, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"	This product includes software developed for the NetBSD Project by
.\"	Wasabi Systems, Inc.
.\" 4. The name of Wasabi Systems, Inc. may not be used to endorse
.\"    or promote products derived from this software without specific prior
.\"    written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd December 16, 2014
.Dt PCI 3
.Os
.Sh NAME
.Nm pci
.Nd library interface for PCI bus access
.Sh LIBRARY
.Lb libpci
.Sh SYNOPSIS
.In pci.h
.Ft int
.Fn pcibus_conf_read "int pcifd" "u_int bus" "u_int dev" "u_int func" \
"u_int reg" "pcireg_t *valp"
.Ft int
.Fn pcibus_conf_write "int pcifd" "u_int bus" "u_int dev" "u_int func" \
"u_int reg" "pcireg_t val"
.Ft int
.Fn pcidev_conf_read "int devfd" "u_int reg" "pcireg_t *valp"
.Ft int
.Fn pcidev_conf_write "int devfd" "u_int reg" "pcireg_t val"
.Ft char *
.Fn pci_findvendor "pcireg_t id_reg"
.Ft void
.Fn pci_devinfo "pcireg_t id_reg" "pcireg_t class_reg" "int showclass" "char *devinfo" "size_t len"
.Ft void
.Fn pci_conf_print "int pcifd" "u_int bus" "u_int dev" "u_int func"
.Ft int
.Fn pci_drvname "int pcifd" "u_int dev" "u_int func" "char *drvname" "size_t len"
.Sh DESCRIPTION
The
.Nm
library provides support for accessing the PCI bus by user programs.
.Pp
These functions are available in the
.Nm libpci
library.
Programs should be linked with
.Fl lpci .
.Sh CONFIGURATION SPACE FUNCTIONS
The following functions are used to access PCI configuration space:
.Bl -tag -width 4n
.It Fn pcibus_conf_read
Access the PCI configuration register
.Fa reg
on the device located at
.Fa bus ,
.Fa dev ,
.Fa func ,
and place the result in
.Fa *valp .
.Fa pcifd
must be an open file descriptor to a PCI bus within the target PCI domain.
.It Fn pcibus_conf_write
Write the value specified by
.Fa val
into the PCI configuration register
.Fa reg
on the device located at
.Fa bus ,
.Fa dev ,
.Fa func .
.Fa pcifd
must be an open file descriptor to a PCI bus within the target PCI domain.
.It Fn pcidev_conf_read
Access the PCI configuration register
.Fa reg
on the device associated with the open file descriptor
.Fa devfd
and place the result in
.Fa *valp .
.It Fn pcidev_conf_write
Write the value specified by
.Fa val
into the PCI configuration register
.Fa reg
on the device associated with the open file descriptor
.Fa devfd .
.El
.Sh MISCELLANEOUS FUNCTIONS
The following miscellaneous functions are available:
.Bl -tag -width 4n
.It Fn pci_findvendor
Return an ASCII description of the PCI vendor in the
PCI ID register
.Fa id_reg .
.It Fn pci_devinfo
Return an ASCII description of the PCI vendor, PCI product,
and PCI class specified by the PCI ID register
.Fa id_reg
and PCI class ID register
.Fa class_reg .
The description is placed into the buffer pointed to by
.Fa devinfo ;
the size of that buffer is specified in
.Fa len .
If
.Fa showclass
is not 0, the class, subclass and interface are added into the buffer.
.It Fn pci_conf_print
Print the PCI configuration information for the device located
at
.Fa bus ,
.Fa dev ,
.Fa func .
.Fa pcifd
must be an open file descriptor to a PCI bus within the target PCI domain.
.It Fn pci_drvname
For the PCI bus opened on
.Fa pcifd ,
return the driver name for
.Fa dev
and
.Fa func
into
.Fa drvname
using
.Fa len
as the buffer length.
.El
.Sh RETURN VALUES
The
.Fn pcibus_conf_read ,
.Fn pcibus_conf_write ,
.Fn pcidev_conf_read ,
.Fn pcidev_conf_write ,
.Fn pci_devinfo ,
and
.Fn pci_drvname
functions return 0 on success and \-1 on failure.
.Pp
The
.Fn pci_findvendor
function returns
.Dv NULL
if the PCI vendor description cannot be found.
.Sh SEE ALSO
.Xr pci 4
.Sh HISTORY
The
.Fn pcibus_conf_read ,
.Fn pcibus_conf_write ,
.Fn pcidev_conf_read ,
.Fn pcidev_conf_write ,
.Fn pci_findvendor ,
.Fn pci_devinfo ,
and
.Fn pci_conf_print
functions first appeared in
.Nx 1.6 .
The
.Fn pci_drvname
function first appeared in
.Nx 7.0 .
